#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

import nmap


class nm:
    def __init__(self,ip):
        self.ip = ip

    def main(self):
        scan_row = []
        scan_row = self.ip.split(' ')
        if len(scan_row) !=1:
            print('ip format error')
            sys.exit()
        hosts = scan_row[0]
        np = nmap.PortScanner()
        try:
            np.scan(hosts=hosts, arguments='-sT', ports='1-65535')
        except Exception as e:
            print(e)
            sys.exit(0)
        for host in np.all_hosts():
            print('----------------------------------------------------')
            print('Host : %s (%s)' % (host, np[host].hostname()))
            print('State : %s' % np[host].state())
            for proto in np[host].all_protocols():
                print('----------')
                print('Protocol : %s' % proto)
                lport = np[host][proto].keys()
                lport.sort()
                for port in lport:
                    print('port : %s\tstate : %s' % (port, np[host][proto][port]['state']))


if __name__ == '__main__':
    if len(sys.argv) != 2:
        print('Usage: python %s <ip>' % sys.argv[0])
        sys.exit()
    nm(sys.argv[1]).main()
